package com.android.syssetting

import android.view.View
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.syssetting.databinding.ActivityUpgradeBinding
import com.android.syssetting.models.UpgradeModel
import com.camera.base.util.APPUtil
import com.camera.base.v.BaseActivity
import com.camera.routemodule.RouterPath
import dagger.hilt.android.AndroidEntryPoint
import java.io.File

@AndroidEntryPoint
@Route(path = RouterPath.path.upgrade_activity)
class UpgradeActivity: BaseActivity<ActivityUpgradeBinding,UpgradeModel>() {
    val TAG = UpgradeActivity::class.java.simpleName

    
    override val viewModelClass: Class<UpgradeModel>
        get() = UpgradeModel::class.java

    override val viewModel: UpgradeModel by viewModels()

    override fun getLayoutId(): Int {
        return R.layout.activity_upgrade
    }

    override fun getStatusBarColor(): Int {
        return R.color.title_background_color
    }

    override fun loadData() {
        mBinding.viewModel = viewModel
        viewModel.showTipsDialogEvent.observe(this) { (show, message) ->
            if (show) {
                showLoadingDialog(resources.getString(message))
            } else {
                hideLoadingDialog()
            }
        }
        viewModel.sureTipsDialogEvent.observe(this){ (show, title, message) ->
            if (show){
                showSureDialog(resources.getString(title)+":"+message,
                    onConfirm = {
                        //开启下载功能
                        if (title == R.string.install_app){
                            APPUtil.installApk(this@UpgradeActivity, viewModel.path)
                        }else{
                            viewModel.downLoadApk()
                        }
                    },
                    onCancel = {
                    })
            }
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        APPUtil.onRequestPermissionsResult(requestCode,permissions,grantResults,this,
            File(viewModel.path)
        )
    }
    
    override fun initView() {

    }

    fun myClick(v: View){
        when(v.id){
            R.id.civ_activity_soft_upgrade -> {
                viewModel.checkVersionUpgrade()
            }
            R.id.btn_activity_upgrade -> {
                viewModel.downLoadApk()
            }
        }
    }
}